package graphics;

import javax.microedition.lcdui.Graphics;

import playlist.Crawler;

import state.State;
import tool.KeyCode;

public class ModeChooser {

	private static final int Y_MENU_OFFSET = 100;
	private static final int X_MENU_OFFSET = 5;
	private Graphics graphics;

	public void repaint(Graphics someGraphics) {
		graphics = someGraphics;
		int height = graphics.getFont().getHeight();

		graphics.setColor(CrawlerColor.WHITE);
		graphics.drawString("1 - Night mode", X_MENU_OFFSET, Y_MENU_OFFSET,
				Graphics.LEFT | Graphics.TOP);
		drawOnOff(State.NIGHT_MODE, X_MENU_OFFSET + graphics.getFont().stringWidth(
				"1 - Night mode"), Y_MENU_OFFSET);
		graphics.drawString("2 - Playlist mode", X_MENU_OFFSET, Y_MENU_OFFSET
				+ height, Graphics.LEFT | Graphics.TOP);
		drawOnOff(State.PLAYLIST_MODE, X_MENU_OFFSET + graphics.getFont().stringWidth(
				"2 - Playlist mode"), Y_MENU_OFFSET + height);
		graphics.drawString("3 - Override mode", X_MENU_OFFSET, Y_MENU_OFFSET
				+ height * 2, Graphics.LEFT | Graphics.TOP);
		drawOnOff(State.OVERRIDE_MODE, X_MENU_OFFSET + graphics.getFont().stringWidth(
				"3 - Override mode"), Y_MENU_OFFSET + height * 2);

	}

	private void drawOnOff(State state, int x, int y) {
		graphics.setColor(Theme.getMenuColor());
		if (Crawler.state().equals(state)) {
			graphics.drawString(" on", x, y, Graphics.LEFT | Graphics.TOP);
		} else {
			graphics.drawString(" off", x, y, Graphics.LEFT | Graphics.TOP);
		}
		graphics.setColor(CrawlerColor.WHITE);
	}

	public void keyPressed(int keyCode) {
		if (keyCode == KeyCode.ONE) {
			switchMode(State.NIGHT_MODE);
			Crawler.resetNightModeIndex();
		} else if (keyCode == KeyCode.TWO) {
			switchMode(State.PLAYLIST_MODE);
		} else if (keyCode == KeyCode.THREE) {
			switchMode(State.OVERRIDE_MODE);
		} else if (keyCode == KeyCode.ASTERISK) {			
			Crawler.state().remove(State.MODE_CHOICE);
		}
	}

	private void switchMode(State state) {
		if (Crawler.state().equals(state)) {
			Crawler.removeState(state);
		} else {
			Crawler.addState(state);
		}
	}

}
